草庐IT

Python float - str - 浮点怪异

全部标签

python - TypeError : list indices must be integers, 不是 str Python

list[s]是一个字符串。为什么这不起作用?出现如下错误:TypeError:listindicesmustbeintegers,notstrlist=['abc','def']map_list=[]forsinlist:t=(list[s],1)map_list.append(t) 最佳答案 当你遍历一个列表时,循环变量接收实际的列表元素,而不是它们的索引。因此,在您的示例中s是一个字符串(第一个abc,然后是def)。看起来您要做的基本上是这样的:orig_list=['abc','def']map_list=[(el,1)f

python - str(list) 如何工作?

为什么str(list)返回我们如何在控制台上看到列表?怎么样str(list)工作?(对str(list)的CPython代码的任何引用)?>>>x=['abc','def','ghi']>>>str(x)"['abc','def','ghi']"从str(list)取回原始列表我必须:>>>fromastimportliteral_eval>>>x=['abc','def','ghi']>>>str(x)"['abc','def','ghi']">>>list(str(x))['[',"'",'a','b','c',"'",',','',"'",'d','e','f',"'",',

python - Numpy 浮点舍入错误

在搜索一些numpy的东西时,我遇到了一个讨论numpy.dot()舍入精度的问题:Numpy:Differencebetweendot(a,b)and(a*b).sum()因为我的table上正好有两台(不同的)带有Haswell-CPU的计算机,它们应该提供FMA和所有东西,我想我会测试Ophion在第一个答案中给出的例子,我得到的结果是让我有些吃惊:更新/安装/修复lapack/blas/atlas/numpy后,我在两台机器上都得到了以下信息:>>>a=np.ones(1000,dtype=np.float128)+1e-14>>>(a*a).sum()1000.0000000

python - 我可以更改 Python 绑定(bind)方法对象的 __str__() 属性吗?

我想更改__str__()我类的一个方法的属性。(注意:不要与“尝试更改方法__str__()”混淆。)我有一个类MyClass,它有一个方法“some_method”。我可以通过以下方式更改MyClass显示自身的方式:classMyClass():def__init__(self):passdefsome_method(self):passdef__str__(self):return"I'maninstanceofMyClass!"当我实例化并打印MyClass时:print(my_class)我得到:I'maninstanceofMyClass!当我print(my_class

python - TypeError:需要一个类似字节的对象,而不是 'str' - python 2 到 3

这个问题在这里已经有了答案:WhydoIneed'b'toencodeastringwithBase64?(5个答案)关闭5年前。您好,我遇到了这条错误消息的问题。我是Python的新手,这个Python2和Python3很麻烦。我不确定在这里做什么,错误消息如下所示。UsingTicker:AAPLTraceback(mostrecentcalllast):File"realtime.py",line18,inr=requests.get(auth_url,headers={"Authorization":"Basic%s"%base64.b64encode(os.environ['

python - 类型 <type 'numpy.string_' > 和 <type 'str' > 有什么区别?

类型之间有区别吗和? 最佳答案 numpy.string_是用于包含固定宽度字节字符串的数组的NumPy数据类型。另一方面,str是原生Python类型,不能用作NumPy数组的数据类型*。如果您创建一个包含字符串的NumPy数组,该数组将使用numpy.string_类型(或Python3中的numpy.unicode_类型)。更准确地说,该数组将使用np.string_的子数据类型。:>>>a=np.array(['abc','xy'])>>>aarray(['abc','xy'],dtype='>>np.issubdtype(

python - 为什么 print ("text"+ str(var1) + "more text"+ str(var2)) 被描述为 "disapproved"?

为什么下面的代码在“SnakesandCoffee”对Blender的Printmultipleargumentsinpython帖子的评论中被称为“古老的不认可打印方法”?是否与Python2或Python3的后端代码/实现有关?print("Totalscorefor"+str(name)+"is"+str(score)) 最佳答案 添加许多字符串不被批准,因为:与其他替代方案相比,它的可读性并不高。它的效率不如其他选择。如果您有其他类型,则必须手动调用它们的str。而且,是的,它真的很旧。:-)理论上,字符串加法会创建一个新字

python - 将字符串转换为列表。 Python [string.split() 行为怪异]

temp="['a','b','c']"printtype(temp)#stringoutput=['a','b','c']printtype(output)#list所以我有这个临时字符串,它基本上是一个字符串格式的列表。..我正试图将它重新变成一个列表,但我不确定这样做的简单方法。我知道一种方法,但我宁愿不使用正则表达式如果我使用temp.split()我得到temp_2=["['a','b','c']"] 最佳答案 使用ast.literal_eval():SafelyevaluateanexpressionnodeoraUn

python - 如何修复此 "TypeError: ' str' 对象不可调用“错误?

我正在创建一个基本程序,该程序将使用GUI获取商品价格,如果初始价格低于10,则减价10%,如果初始价格低于10,则减价20%初始价格大于十:importeasyguiprice=easygui.enterbox("Whatisthepriceoftheitem?")iffloat(price)10:easygui.msgbox("Yournewpriceis:$"(float(price)*0.2))虽然我一直收到这个错误:easygui.msgbox("Yournewpriceis:$"(float(price)*0.1))TypeError:'str'objectisnotcal

python - 子进程 "TypeError: a bytes-like object is required, not ' str'"

我正在使用来自apreviouslyaskedquestionafewyearsago的代码但是,我认为这已经过时了。尝试运行代码时,我收到了上面的错误。我仍然是Python的新手,所以我无法从类似的问题中得到太多的澄清。有谁知道为什么会这样?importsubprocessdefgetLength(filename):result=subprocess.Popen(["ffprobe",filename],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)return[xforxinresult.stdout.readlines()if"